package com.controller;

import com.domain.Product;
import com.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * 项目名称：ssm21
 * 类 名 称：ProductController
 * 类 描 述：TODO
 * 创建时间：2020-01-15 10:26
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService iProductService;

    // 查询所有产品
    @RequestMapping("/findAll.do")
    @RolesAllowed("ADMIN")
    ModelAndView findAll() throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Product> all = iProductService.findAll();
        modelAndView.addObject("productList", all);
        modelAndView.setViewName("product-list");
        return modelAndView;
    }

    // 添加产品
    @RequestMapping("/save.do")
    public String save(Product product) throws Exception {
        iProductService.save(product);
        return "redirect:findAll.do";
    }

}
